<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Company extends Model
{
    public function getcompany($res)
    {
        $map = array();
        //normal 正常   draft草稿 lock 锁定  del删除
        if (isset($res['status']) && $res['status']) {
            $map['a.status'] = $res['status'];
        }
        //公司名、地址、邮编
        if (isset($res['dname']) && $res['dname']) {
            $maps['a.name'] = array('like', '%' . removeBlank($res['dname']) . '%');//公司名
            $maps['b.addr'] = array('like', '%' . removeBlank($res['dname']) . '%');//地址
            $maps['b.zipcode'] = array('like', '%' . removeBlank($res['dname']) . '%');//邮编
        } else {
            $maps = array();
        }
        //国家搜索
        if (isset($res['country_id']) && $res['country_id']) {
            $map['b.country_id'] = $res['country_id'];
        }
        //省份搜索
        if (isset($res['provinces_id']) && $res['provinces_id']) {
            $map['b.province_id'] = $res['provinces_id'];
        }
        //公司分类搜索
        if (isset($res['type_id']) && $res['type_id']) {
            $map['a.type_id'] = $res['type_id'];
        }
        $datares = $this->alias('a')->join('nt_co_profile b', 'a.id=b.co_id', 'left')->where(function ($query) use ($map) {
            $query->where($map);
        })->where(function ($query) use ($maps) {
            $query->whereOr($maps);
        })->field('a.*,b.works,b.country_id,b.province_id,b.city_id,b.town_id,b.addr,b.zipcode')->order('a.create_at')->page($res['page'], $res['pageSize'])->select();
        //获取其他的数据
        $langs = $res['lang'];
        foreach ($datares as $datareskey => $dataresval) {
            //公司类型
            $cotypewhere['mkey'] = 'cotype_' . $dataresval['type_id'];
            $datares[$datareskey]['cotypename'] = getlangname('multi_lang', $cotypewhere, $langs);
            //业务员
            $datares[$datareskey]['managename'] = getinfo($dataresval['saler_id'], 'admin')['nickname'];
            $datares[$datareskey]['update_date'] = getTimeDate($dataresval['update_at'],12);
            //时间
            if ($langs != 'zh') {
                $time=explode(' ', getTimeDate($dataresval['active_day'], 9));
                //活动时间
               $datares[$datareskey]['active_timeone'] = $time[0];
                 $datares[$datareskey]['active_timetwo'] = $time[1];

            } else {
                $times=explode(' ', getTimeDate($dataresval['active_day']),12);
                //活动时间
              $datares[$datareskey]['active_timeone'] = $times[0];
               $datares[$datareskey]['active_timetwo'] = $times[1];
            }
            //拼接用户的地址
            $country = getinfo($dataresval['country_id'], 'country')['name'];//国家
            $provinces = getinfo($dataresval['province_id'], 'provinces')['name'];//省
            $city = getinfo($dataresval['city_id'], 'city')['name'];//城市
            $town = getinfo($dataresval['town_id'], 'town')['name'];//镇
            //地址
            $datares[$datareskey]['address'] = $country . ' ' . $provinces . ' ' . $city . ' ' . $town;
            $datares[$datareskey]['works'] = $this->worksexdata($dataresval['works'], 1);//员工数量
        }

        $data['data'] = $datares;
        //当前页的ids
        $data['ids'] = array_column($datares, 'id');
        $data['count'] = $this->alias('a')->join('nt_co_profile b', 'a.id=b.co_id', 'left')->where(function ($query) use ($map) {
            $query->where($map);
        })->where(function ($query) use ($maps) {
            $query->whereOr($maps);
        })->count();
        return $data;
    }

    //处理客户的删除与锁定的接口‘
    public function ulockadjustrecover($data)
    {
        //normal 正常   draft草稿 lock 锁定  del删除
        $ids = $data['id'];
        if ($ids) {
            //单独获取这个查询数据的状态
            $deldata = Db::name('company')->field('status,name')->where("id in($ids)")->select();
            $arrstatus = array_column($deldata, 'status');//状态
            $arrname = array_column($deldata, 'name');//公司名
            if (count($arrname) > 3) {
                $namecompany = $arrname[0] . $arrname[1] . $arrname[2] . '...';
            } else {
                $namecompany = implode(",", $arrname);
            }
        }
        if ($data['act'] == 'lock') {
            if (!$ids) {
                return array('msg' => lang("please_select_a_company_to_lock"), 'status' => 3);
            } else {
                $update['status'] = 'LOCK';//锁定
                $update['update_id'] = session('userid');//更新用户
                $update['update_at'] = time();//更新时间
                $ret = $this->where("id in($ids)")->update($update);
                $success = $namecompany . lang("locked_successfully");
                $failed = $namecompany . lang("lock_failed");
            }
        } elseif ($data['act'] == 'del') {
            //伪删除
            if (!$ids) {
                return array('msg' => lang("please_select_a_company_to_delete"), 'status' => 3);
            } else {
                //锁定的公司不能删除
                if (in_array('LOCK', $arrstatus)) {
                    return array('msg' => $namecompany . lang("it's_locked_can't_be_deleted"), 'status' => 0);
                    exit;
                }
                $update['status'] = 'DEL';//删除
                $update['delete_id'] = session('userid');//删除的用户
                $update['del_at'] = time();
                $ret = $this->where("id in($ids)")->update($update);
                $success = $namecompany . lang("deleted_successfully");
                $failed = $namecompany . lang("failed_to_delete");
            }
        } elseif ($data['act'] == 'unlock') {
            if (!$ids) {
                return array('msg' => lang("please_select_a_company_to_unlock"), 'status' => 3);
            } else {
                $update['update_id'] = session('userid');//更新用户
                $update['update_at'] = time();//更新时间
                $update['status'] = 'NORMAL';//正常
                $ret = $this->where("id in($ids)")->update($update);
                $success = $namecompany . lang("unlock_successful_");
                $failed = $namecompany . lang("unlock_failed");
            }
        } elseif ($data['act'] == 'recover') {
            if (!$ids) {
                return array('msg' => lang("please_select_a_company_to_restore"), 'status' => 3);
            } else {
                if (in_array('LOCK', $arrstatus)) {
                    return array('msg' => $namecompany . lang("status_locked_can't_be_recovered"), 'status' => 0);
                    exit;
                }
                $update['update_id'] = session('userid');//更新用户
                $update['update_at'] = time();//更新时间
                $update['status'] = 'NORMAL';//正常
                $ret = $this->where("id in($ids)")->update($update);
                $success = $namecompany . lang("recovery_successful");
                $failed = $namecompany . lang("recovery_failed");
            }
        } elseif ($data['act'] == 'clear') {
            if (!$ids) {
                return array('msg' => lang("please_select_a_company_to_clear"), 'status' => 3);
            } else {
                if (in_array('LOCK', $arrstatus)) {
                    return array('msg' => $namecompany . lang("it's_locked_can't_be_deleted"), 'status' => 0);
                    exit;
                }
                //数据的真正的删除
                $ret = $this->where("id in($ids)")->delete();
                linkbankhandle($ids);
                $success = $namecompany . lang("cleared_successfully");
                $failed = $namecompany . lang("clear_failed");
            }
        }
        if ($ret === false) {
            return array('status' => 0, 'msg' => $failed);
        } else {
            return array('status' => 1, 'msg' => $success);
        }
    }

    //处理公司的添加和公司的接口
    public function addeditcompany($data)
    {
        $admin = session('admin');
        $ckdata = notEmpty(['type_id' => lang("company_type"), 'name' => lang("company_name"), 'saler_id' => lang("employees"), 'works' => lang("number_of_employees"), 'zipcode' => lang("ZIP_CODE")]);
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        if (!$data['address']['country_id'] || !$data['address']['province_id'] || !$data['address']['city_id']) {
            return array('msg' => lang("address_can't_be_empty"), 'status' => 2);
        }
        if (!isset($data['linkdata'])) {
            return array('msg' => lang("contact_can't_be_empty"), 'status' => 2);
        }
        if (!isset($data['bankdata'])) {
            return array('msg' => lang("bank_account_can't_be_empty"), 'status' => 2);
        }
        $data['status'] = $data['statuss'];
        $data['active_day'] = time();//活动时间
        $data['trust'] = '1';//信任程度
        $data['country_id'] = $data['address']['country_id'];//国家id
        $data['province_id'] = $data['address']['province_id'];//省份id
        $data['city_id'] = $data['address']['city_id'];//城市id
        $data['town_id'] = $data['address']['town_id'];//城镇id
        if ($data['act'] == 'companyadd') {
            $data['update_id'] = $data['create_id'] = session('userid');
            $data['create_at'] = $data['update_at'] = time();
            Db::startTrans();
            $companyid = $this->strict(false)->insertGetId($data);//公司表
            $data['co_id'] = $companyid;//公司id
            $comprofileid = Db::name('co_profile')->strict(false)->insertGetId($data);//公司详情表
            if (isset($data['attrval']) && $data['attrval']) {
                $companycarr['attrval'] = $data['attrval'];//公司属性
                $companycarr['co_id'] = $companyid;//公司id
                //公司属性表
                $companycarid = $this->companycoattr($companycarr, 1);
            }
            //联系人和银行的更新，公司id更改成提交现在的公司id
            Db::name('linkman')->where('co_id', 0)->setField('co_id', $companyid);
            Db::name('co_bank')->where('co_id', 0)->setField('co_id', $companyid);
            if ($companyid && $comprofileid) {
                Db::commit();
                return array('msg' => $data['name'] . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("add_failed"), 'status' => 0);
            }
        } elseif ($data['act'] == 'companyedit') {
            $info = $this->where('id', $data['co_id'])->find();
            if ($info['status'] == 'LOCK') {
                return array('msg' => $data['name'] . lang("status_locked_can't_be_edit"), 'status' => 0);
            }
            //判断是否有权限编辑
            $cotypepos = explode(",", getinfo($info['type_id'], 'cotype')['position_ids']);
            //编辑
            $updata = array(
                'update_id' => session('userid'),
                'update_at' => time(),
                'status' => $data['statuss'],
                'type_id' => $data['type_id'],
                'name' => $data['name'],
                'saler_id' => $data['saler_id'],
            );
            $upprodata = array(
                'remarks' => $data['remarks'],
                'works' => $data['works'],
                'country_id' => $data['country_id'],
                'province_id' => $data['province_id'],
                'city_id' => $data['city_id'],
                'town_id' => $data['town_id'],
                'addr' => $data['addr'],
                'zipcode' => $data['zipcode'],
            );
            Db::startTrans();
            $companyupid = $this->where('id', $data['co_id'])->update($updata);//公司表
            $profileid = Db::name('co_profile')->where('co_id', $data['co_id'])->update($upprodata);//公司详情表
            if (isset($data['attrval']) && $data['attrval']) {
                $companycarr['attrval'] = $data['attrval'];//公司属性
                $companycarr['co_id'] = $data['co_id'];//公司id
                //公司属性表
                $companycarid = $this->companycoattr($companycarr, 2);
            }
            //公司的编辑（联系人个银行无需更新）
            if ($companyupid !== false && $profileid !== false) {
                Db::commit();
                return array('msg' => $data['name'] . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("edit_failed"), 'status' => 0);
            }
        }

    }

    //处理公司属性的添加
    protected function companycoattr($data, $code)
    {
        $companyid = $data['co_id'];//公司id
        $dataselecttext = $data['attrval'];
        Db::name('coattr_value')->where('co_id', $data['co_id'])->delete();
        if (isset($dataselecttext['text']) && $dataselecttext['text']) {
            foreach ($dataselecttext['text'] as $key => $val) {
                $textdata['co_id'] = $data['co_id'];
                $textdata['content'] = $val['val'];
                $textdata['coattr_id'] = $val['key'];
                Db::name('coattr_value')->insert($textdata);
            }
        }
        if ($code == 2) {
            if (isset($dataselecttext['select']) && $dataselecttext['select']) {
                foreach ($dataselecttext['select'] as $key => $val) {
                    $selectdata['co_id'] = $data['co_id'];
                    $selectdata['content'] = $val['val'];
                    $selectdata['coattr_id'] = $val['id'];
                    Db::name('coattr_value')->insert($selectdata);
                }
            }
        } else {
            if (isset($dataselecttext['select']) && $dataselecttext['select']) {
                foreach ($dataselecttext['select'] as $key => $val) {
                    $selectdata['co_id'] = $data['co_id'];
                    $selectdata['content'] = $val['val'];
                    $selectdata['coattr_id'] = $val['key'];
                    Db::name('coattr_value')->insert($selectdata);
                }
            }
        }
    }

//处理公司详情数据的接口
    public function getdetail($data)
    {
        $admin = session('admin');
        $lang = $data['lang'];//语言
        $id = '';//公司id
        if ($data['status'] != 'companyadd') {
            $id = $data['company_id'];//公司id
        }
        if (isset($data['status']) && $data['status'] == 'detail') {
            //点击详情获取的数据
            $infodata = Db::name('company')->alias('a')->join('nt_co_profile b', 'a.id=b.co_id', 'left')->field('a.*,b.works,b.country_id,b.province_id,b.city_id,b.town_id,b.addr,b.zipcode,b.remarks')->where('a.id', $id)->find();
            //公司类型
            $wherecotype['mkey'] = 'cotype_' . $infodata['type_id'];
            $infodata['cotypename'] = getlangname('multi_lang', $wherecotype, $lang);
            //业务员
            $infodata['managename'] = getinfo($infodata['saler_id'], 'admin')['nickname'];
            //添加者
            $infodata['createname'] = getinfo($infodata['create_id'], 'admin')['nickname'];
            //编辑者
            $infodata['updatename'] = getinfo($infodata['update_id'], 'admin')['nickname'];
            //员工数量
            $infodata['worksnum'] = $this->worksexdata($infodata['works'], 1);
            //拼接用户地址
            $country = getinfo($infodata['country_id'], 'country')['name'];//国家
            $provinces = getinfo($infodata['province_id'], 'provinces')['name'];//省
            $city = getinfo($infodata['city_id'], 'city')['name'];//城市
            $town = getinfo($infodata['town_id'], 'town')['name'];//镇
            if ($provinces == $city) {
                $prci = ' ' . $provinces . ' ';
            } else {
                if ($lang != 'zh') {
                    $prci = ' ' . $city . ' ' . $provinces . ' ';
                } else {
                    $prci = ' ' . $provinces . ' ' . $city . ' ';
                }
            }
            //编辑时间
            $infodata['update_date'] = getTimeDate($infodata['update_at'],12);
            if ($lang != 'zh') {
                $infodata['address'] = $town . $prci . $country;
                //添加时间
                $infodata['create_date'] = getTimeDate($infodata['create_at'], 9);
            } else {
                //添加时间
                $infodata['create_date'] = getTimeDate($infodata['create_at'],12);
                $infodata['address'] = $country . $prci . $town;
            }
            //获取对应的公司的属性名称
            $selectdata = $textdata = array();
            $attrdata = array_unique(array_column(Db::name('coattr_value')->where('co_id', $id)->select(), 'coattr_id'));//属性id（去重）
            foreach ($attrdata as $attrdatakey => $attrdataval) {
                //判断属性是select还是文本框
                $coattrinfo = Db::name('coattr')->field('id,is_text,mkey')->where('id', $attrdataval)->find();
                if ($coattrinfo['is_text'] == 1) {
                    //select框
                    $wherecoattr['mkey'] = 'coattr_' . $coattrinfo['id'];
                    //选择框,获取对应的属性名称
                    $getlangname = getlangname('multi_lang', $wherecoattr, $lang);
                    array_push($selectdata, $getlangname);//selectde 的值
                } else {
                    //文本框
                    $wherecoattr['mkey'] = 'coattr_' . $coattrinfo['id'];
                    $getlangname = getlangname('multi_lang', $wherecoattr, $lang);
                    array_push($textdata, $getlangname);//selectde 的值
                }
            }
            $infodata['textdata'] = $textdata;//文本框
            $infodata['selectdata'] = $selectdata;//select框
            $resdata = array('data' => $infodata);
        } elseif (isset($data['status']) && $data['status'] == 'linkman') {
            if ($id) {
                $maplink['co_id'] = $id;//公司id
            } else {
                //添加时，联系人的展示
                $maplink['co_id'] = 0;
            }
            //获取公司的联系人
            $list = Db::name('linkman')->where($maplink)->page($data['page'], $data['pageSize'])->select();
            foreach ($list as $key => $value) {
                //是否主要
                if ($value['is_main'] == 1) {
                    $list[$key]['ismans'] = 'Y';
                } else {
                    $list[$key]['ismans'] = 'N';
                }
                //性别
                $list[$key]['sex'] = $this->worksexdata($value['sex'], 2);
                //编辑时间
                $list[$key]['update_date'] = getTimeDate($value['update_at'],12);
                //编辑者
                $list[$key]['updatename'] = getinfo($value['update_id'], 'admin')['nickname'];
                //通过联系人的id，获取该联系人的方式
                $linkcon = Db::name('linkman_method')->where('linkman_id', $value['id'])->select();
                $namecontent = array();
                //获取联系方式的名称
                foreach ($linkcon as $linkey => $linvalue) {
                    //获取这个用户的联系方式名称
                    $namecontent[] = getinfo($linvalue['coattr_id'], 'coattr_select')['option_text'] . ':' . $linvalue['content'] . '<br/>';
                }
                //联系，名称
                $list[$key]['contact'] = implode('', $namecontent);
            }
            $resdata['data'] = $list;
            $resdata['ids'] = array_column($list, 'id');
            $resdata['count'] = Db::name('linkman')->where($maplink)->count();
        } elseif (isset($data['status']) && $data['status'] == 'bank') {
            if ($id) {
                $mapbank['a.co_id'] = $id;//公司id
            } else {
                //添加时，联系人的展示
                $mapbank['a.co_id'] = 0;
            }

            //获取所有的联系人
            $join = [
                ['nt_banks b', 'a.banks_id=b.id']
            ];
            $list = Db::name('co_bank')->alias('a')->join($join)->field('a.*,b.name')->where($mapbank)->page($data['page'], $data['pageSize'])->select();
            foreach ($list as $key => $value) {
                //编辑时间
                $list[$key]['update_date'] = getTimeDate($value['update_at'],12);
                //编辑者
                $list[$key]['updatename'] = getinfo($value['update_id'], 'admin')['nickname'];
                //是否主要
                if ($value['is_main'] == 1) {
                    $list[$key]['ismans'] = 'Y';
                } else {
                    $list[$key]['ismans'] = 'N';
                }
            }
            $resdata['data'] = $list;
            $resdata['ids'] = array_column($list, 'id');
            $resdata['count'] = Db::name('co_bank')->alias('a')->join($join)->where($mapbank)->count();
        } elseif (isset($data['status']) && $data['status'] == 'companyedit') {
            //编辑数据的接口
            $where['a.lang'] = $lang;//语言标志
            $posid = $admin['position_id'];
            if ($posid != 1) {
                $where[] = ["exp", "FIND_IN_SET($posid,position_ids)"];
            }
            $join = [
                ['nt_cotype b', 'a.mkey=b.mkey']
            ];
            $table = 'multi_lang';
            $cotypename = getmultidata($table, $join, $where)['data'];
            //接口
            $textarr = $selectnamearr = array();
            $info = Db::name('company')->alias('a')->join('nt_co_profile b', 'a.id=b.co_id', 'left')->field('a.*,b.works,b.country_id,b.province_id,b.city_id,b.town_id,b.addr,b.zipcode,b.remarks')->where('a.id', $id)->find();
            //获取公司的的属性值
            $info['coattrdata'] = Db::name('coattr_value')->where('co_id', $id)->select();
            //获取业务部下的成员
            $managename = getables('admin', ['position_id' => 1], 'id,nickname');
            //国家
            $countryname = getables('country', null, 'id,name');
            $works = array('1' => '1-10', '2' => '11-20', '3' => '21-50', '4' => '51-100', '5' => '100+');
            //编辑返回的数据
            $resdata = array('cotypename' => $cotypename, 'managename' => $managename, 'countryname' => $countryname, 'lang' => $lang, 'info' => $info, 'works' => $works);
        } elseif (isset($data['status']) && $data['status'] == 'companyadd') {
            $where['a.lang'] = $lang;//语言标志
            $posid = $admin['position_id'];
            if ($posid != 1) {
                $where[] = ["exp", "FIND_IN_SET($posid,position_ids)"];
            }
            $join = [
                ['nt_cotype b', 'a.mkey=b.mkey']
            ];
            $table = 'multi_lang';
            //公司类型
            $cotypename = getmultidata($table, $join, $where)['data'];
            //业务员的id和名称
            $managename = getables('admin', ['position_id' => 1], 'id,nickname');
            //国家
            $countryname = getables('country', null, 'id,name');
            $works = array('1' => '1-10', '2' => '11-20', '3' => '21-50', '4' => '51-100', '5' => '100+');
            //添加返回数据
            $resdata = array('cotypename' => $cotypename, 'managename' => $managename, 'countryname' => $countryname, 'lang' => $lang, 'works' => $works);
        }
        return $resdata;
    }

//处理员工的数量的显示
    protected function worksexdata($ststus, $code)
    {
        switch ($ststus) {
            case '0':
                $sex = lang("woman");
                break;
            case '1':
                $works = '1-10';
                $sex = lang("man");
                break;
            case '2':
                $works = '11-20';
                break;
            case '3':
                $works = '21-50';
                break;
            case '4':
                $works = '51-100';
                break;
            case '5':
                $works = '100+';
                break;
        }
        if ($code == 1) {
            return $works;
        } elseif ($code == 2) {
            return $sex;
        }
    }

    public function indocoattrss($res)
    {
        $cotype = $res['cotype'];//分类名称
        $rescoa = array();
        //匹配公司类型属性
        $where['b.cotype_ids'] = array('like', '%' . $cotype . '%');
        $where['b.person'] = 0;//使用公司的属性
        //获取分类名称
        $where['a.lang'] = $res['lang'];//语言标志
        $join = [
            ['nt_coattr b', 'a.mkey=b.mkey']
        ];
        $table = 'multi_lang';
        $coarr = getmultidata($table, $join, $where)['data'];//属性信息
        //获取对应的属性中值，select,text
        foreach ($coarr as $cokey => $covalue) {
            if ($covalue['is_text'] == 1) {
                //select框
                $res = Db::name('coattr_select')->where('coattr_id', $covalue['id'])->select();
                foreach ($res as $reskey => $resvalue) {
                    $coarr[$cokey]['textselect'][] = array('content' => $resvalue['option_text'], 'id' => $resvalue['id']);
                }
            } else if ($covalue['is_text'] == 0) {
                //文本框
                $coarr[$cokey]['textselect'][] = array();
            }
        }
        //对获取的select进行处理
        return $coarr;
    }

    //员工的批量调整接口显示
    public function getcompanydatas($data)
    {
        $ids = $data['ids'];
        //查询要批量编辑的的数据
        $datares = $this->alias('a')->join('nt_co_profile b', 'a.id=b.co_id', 'left')->where("a.id in($ids)")->field('a.*,b.works,b.country_id,b.province_id,b.city_id,b.town_id,b.addr')->page($data['page'], $data['pageSize'])->select();
        $langs = $data['lang'];
        foreach ($datares as $datareskey => $dataresval) {
            //公司类型
            $cotypewhere['mkey'] = 'cotype_' . $dataresval['type_id'];
            $datares[$datareskey]['cotypename'] = getlangname('multi_lang', $cotypewhere, $langs);
            //业务员
            $datares[$datareskey]['managename'] = getinfo($dataresval['saler_id'], 'admin')['nickname'];
            //拼接用户的地址
            $country = getinfo($dataresval['country_id'], 'country')['name'];//国家
            $provinces = getinfo($dataresval['province_id'], 'provinces')['name'];//省
            $city = getinfo($dataresval['city_id'], 'city')['name'];//城市
            $town = getinfo($dataresval['town_id'], 'town')['name'];//镇
            //地址
            $datares[$datareskey]['address'] = $country . ' ' . $provinces . ' ' . $city . ' ' . $town;
            $datares[$datareskey]['works'] = $this->worksexdata($dataresval['works'], 1);//员工数量
        }
        $data['data'] = $datares;
        $data['countrys'] = getables('country');
        $data['works'] = array('1' => '1-10', '2' => '11-20', '3' => '21-50', '4' => '51-100', '5' => '100+');//员工数量
        $data['managename'] = getables('admin', ['position_id' => 1], 'id,nickname');//指定业务员
        //当前页的ids
        $data['ids'] = array_column($datares, 'id');
        $data['count'] = $this->alias('a')->join('nt_co_profile b', 'a.id=b.co_id', 'left')->where("a.id in($ids)")->count();
        return $data;
    }

    //公司的批量调整
    public function batchall($data)
    {
        $ids = $data['ids'];
        $comid = '';
        if (!$ids) {
            return array('msg' => lang("please_select_a_company_to_batch_adjustment"), 'status' => 0);
            exit;
        } else {
            $ddata = Db::name('company')->field('name')->where("id in($ids)")->select();
            $arrname = array_column($ddata, 'name');//公司名
            if (count($arrname) > 3) {
                $namecompany = $arrname[0] . $arrname[1] . $arrname[2] . '...';
            } else {
                $namecompany = implode(",", $arrname);
            }
        }
        //国家
        if (isset($data['country_id']) && $data['country_id']) {
            $comid = Db::name('co_profile')->where('co_id', 'in', $data['ids'])->setField('country_id', $data['country_id']);
        }
        //省份
        if (isset($data['province_id']) && $data['province_id']) {
            $comid = Db::name('co_profile')->where('co_id', 'in', $data['ids'])->setField('province_id', $data['province_id']);
        }
        //城市
        if (isset($data['city_id']) && $data['city_id']) {
            $comid = Db::name('co_profile')->where('co_id', 'in', $data['ids'])->setField('city_id', $data['city_id']);
        }
        //城镇
        if (isset($data['town_id']) && $data['town_id']) {
            $comid = Db::name('co_profile')->where('co_id', 'in', $data['ids'])->setField('town_id', $data['town_id']);
        }
        //员工数量
        if (isset($data['works']) && $data['works']) {
            $comid = Db::name('co_profile')->where('co_id', 'in', $data['ids'])->setField('works', $data['works']);
        }
        if (isset($data['saler_id']) && $data['saler_id']) {
            //指定业务员
            $comid = $this->where('id', 'in', $data['ids'])->setField('saler_id', $data['saler_id']);
        }
        if (!$comid) {
            return array('msg' => lang("please_select_the_type_to_edit"), 'status' => 0);
        } else {
            if ($comid !== false) {
                return array('msg' => $namecompany . lang("batch_adjustment_successful"), 'status' => 1);
            } else {
                return array('msg' => $namecompany . lang("batch_adjustment_failed"), 'status' => 0);
            }
        }
    }

    //获取公司的名称
    public function getcompanyname($data)
    {
        $map['name'] = ['like', '%' . $data['dname'] . '%'];
        $map['status'] = 'NORMAL';
        $data = $this->field('id,name')->where($map)->select();
        return $data;
    }
}


